package com.dao.Impl;

import com.dao.GoodsDao;
import com.entity.Goods;
import com.entity.PageBean;
import com.util.JDBC;
import com.util.TransBean;

import java.util.List;
import java.util.Map;

/**
 * @author 沐白
 */
public class GoodsDaoImpl implements GoodsDao {
    /**
     * 添加商品
     *
     * @param goods
     * @return
     */
    @Override
    public int addGoods(Goods goods) {
        int i = selectMaxId();
        return JDBC.sqlUpdate("insert into goods(id,goodsId,name,code,status,type,unitId,price,categoryId,supplieId,purprc,numstatus) values(null,?,?,0,1,?,?,?,?,?,?,?)",
                i + 1001,
                goods.getName(),
                goods.getType(),
                goods.getUnitId(),
                goods.getPrice(),
                goods.getCategoryId(),
                goods.getSupplieId(),
                goods.getPurprc(),
                1
        );
    }

    @Override
    public int selectMaxId() {
        List<List> lists = JDBC.selectArray("select max(id) from goods");
        if (lists.size() > 0) {
            Integer id = (Integer) lists.get(0).get(0);
            return id;
        }
        return -1;
    }

    /**
     * 修改商品
     *
     * @param goods
     * @return
     */
    @Override
    public int modify(Goods goods) {
            return JDBC.sqlUpdate("update goods set name=?,code=?,status=?,type=?,unitId=?,price=?,categoryId=? where id=?",
                    goods.getName(),
                    goods.getCode(),
                    goods.getStatus(),
                    goods.getType(),
                    goods.getUnitId(),
                    goods.getPrice(),
                    goods.getCategoryId(),
                    goods.getId()
            );
    }

    /**
     * 修改商品状态
     *
     * @param goods
     * @return
     */
    public int modifyStatus(Goods goods) {
            return JDBC.sqlUpdate("update goods set status=? where id=?",
                    goods.getStatus(),
                    goods.getId()
            );
    }

    /**
     * 查询全部
     *
     * @return
     */
    @Override
    public List<Goods> findAll() {
            List<Map> maps = JDBC.selectMap("select id,goodsId,name,code,status,type,unitId,price,categoryId from goods");
        List<Goods>   list = TransBean.populate(Goods.class,maps);
        return list;
    }

    @Override
    public void findAllStatus(PageBean page) {
        String sql ="select count(*) from goods where status=1 %s ";
        String whereStr = "";
        String[] factor = page.getFactor();
        if(factor!=null){
            for (String s : factor) {
                whereStr+=" and "+s;
            }
        }
        sql=String.format(sql, whereStr);
        List<List> list = JDBC.selectArray(sql);
        int count = ((Long) list.get(0).get(0)).intValue();
        page.setRowCount(count);

        sql ="select id,goodsId,name,code,status,type,unitId,price,categoryId " +
                "from goods where status=1 %s limit ?,?";
        sql=String.format(sql, whereStr);
        List<Map> maps = JDBC.selectMap(sql,page.getStartIndex(),page.getPageSize());
        page.setData(TransBean.populate(Goods.class,maps));
    }

    @Override
    public void findSctn(PageBean page) {

        String sql = ("select count(*) from goods where 1=1 ");
        if (page.getFactor() != null) {
            sql += page.getFactor()[0];
        }
        List<List> lists = JDBC.selectArray(sql);
        page.setRowCount(((Long) lists.get(0).get(0)).intValue());
        page.setTotalPage();
        page.setStartIndex();

        sql = ("select id,goodsId,name,code,status,type,unitId,price,categoryId from goods where 1=1 ");
        if (page.getFactor() != null) {
            sql += page.getFactor()[0];
        }
        sql += " limit ?,?";
        List<Map> map = JDBC.selectMap(sql, page.getStartIndex(), page.getPageSize());
        List<Goods> list = TransBean.populate(Goods.class, map);
        page.setData(list);

    }

    /**
     * 模糊查询
     *
     * @param goodsName
     * @return
     */
    @Override
    public List<Goods> findByName(String goodsName) {
        List<Map> lists = JDBC.selectMap("select id,goodsId,name,code,status,type,unitId,price,categoryId from goods where name like '%" + goodsName + "%' ");
        if (lists.size() > 0) {
            List<Goods> list = TransBean.populate(Goods.class, lists);
            return list;
        }
        return null;
    }

    @Override
    public Goods findById(int goodId) {
        List<List> lists  = JDBC.selectArray("select id,goodsId,name,code,status,type,unitId,price,categoryId from goods where id=?", goodId);
        if (lists.size() > 0) {
            List li = lists.get(0);
            return new Goods(
                    (Integer) li.get(0),
                    (String) li.get(1),
                    (String) li.get(2),
                    (String) li.get(3),
                    (Integer) li.get(4),
                    (Integer) li.get(5),
                    (Integer) li.get(6),
                    (Double) li.get(7),
                    (Integer) li.get(8)
            );
        }
        return null;
    }

    @Override
    public Goods findByGoodsId(int goodId) {
        List<Map> maps  = JDBC.selectMap("select id,goodsId,name,code,status,type,unitId,price,categoryId,supplieId from goods where goodsId=?",
                goodId);
        if(maps!=null&&maps.size()>0){
            List<Goods> olist = TransBean.populate(Goods.class, maps);
            return olist.get(0);
        }
        return null;
    }

    @Override
    public List<Map> findName() {
        return JDBC.selectMap("select name from goods");
    }

    @Override
    public List<Goods> search(String goodsName) {
        List<Map> mapList = JDBC.selectMap("select name,id from goods where name like '%" + goodsName + "%'");
        return TransBean.populate(Goods.class, mapList);
    }

    @Override
    public int updateByNameCount(String name, int count) {
        return JDBC.sqlUpdate("update goods set code=code+? where name=?",count,name);
    }

    @Override
    public int updateByNameNumstatus(String name) {
        return  JDBC.sqlUpdate("update goods set numstatus=0 where name=?",name);
    }

    @Override
    public int updateByNameCountjian(int goodsId, int count) {
        return JDBC.sqlUpdate("update goods set code=code-? where goodsId=?",count,goodsId);
    }

    @Override
    public int updateNumstatus(int goodsId) {
        return JDBC.sqlUpdate("update goods set numstatus=1 where goodsId=? and numstatus=0 and code<=50",goodsId);
    }

    @Override
    public void findGoods(PageBean page) {
        String sql="select count(*) from goods g,category c where g.categoryId=c.Id and g.`status`=1 and g.supplieId is not null ";
        String condstr="";
        if (page.getFactor()!=null){
            for (String s : page.getFactor()) {
                condstr+=" and "+s;
            }
        }
        page.setStartIndex();
        List<List> lists=JDBC.selectArray(sql+(condstr==null?"":condstr));
        page.setRowCount(((Long)lists.get(0).get(0)).intValue());
        page.getTotalPage();
        sql=String.format("select g.id,goodsId,g.name name1,supplieName,g.price,g.purprc,c.name,g.code,c.id cid,g.numstatus from goods g,supplie s,category c where g.supplieId=s.id and g.categoryId=c.Id and g.`status`=1 and g.supplieId is not null %s order by (g.code+0) asc limit ?,?",(condstr==null?"":condstr));
        List<Map> listList=JDBC.selectMap(sql,(page.getPageNo() - 1) * page.getPageSize(), page.getPageSize());
        if (listList.size()>0){
            page.setData(listList);
        }

    }

    @Override
    public int chengeNum(int googsId, int codeNum) {
        return JDBC.sqlUpdate("update goods set code=code-? where goodsId=?",googsId);
    }

    @Override
    public int alterNumstatus(int goodsId) {
        return JDBC.sqlUpdate("update goods set numstatus=2 where goodsId=?",goodsId);
    }

    @Override
    public int modifyNumStatus(int goodsId) {
        return JDBC.sqlUpdate("update goods set numstatus=1 where goodsId=?", goodsId);
    }

}
